/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.hcht.xrlh.tj.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.google.common.collect.Lists;
import com.hcht.xrlh.tj.entity.DataStatistics;
import com.hcht.xrlh.tj.entity.Series;
import com.hcht.xrlh.tj.entity.TjZheXianQuery;
import com.hcht.xrlh.tj.entity.TjZhu;
import com.hcht.xrlh.tj.entity.TjZhuQuery;
import com.hcht.xrlh.tj.service.DataStatisticsService;
import com.hcht.xrlh.tj.service.TjZheXianQueryService;
import com.hcht.xrlh.tj.service.TjZhuQueryService;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.sys.entity.Office;
import com.thinkgem.jeesite.modules.sys.entity.User;
import com.thinkgem.jeesite.modules.sys.service.OfficeService;

/**
 * 统计Controller
 * @author lrz
 * @version 2016-12-19
 */
@Controller
@RequestMapping(value = "${adminPath}/tj/tj")
public class TjController extends BaseController {

	@Autowired
	OfficeService officeService;
	@Autowired
	TjZhuQueryService tjZhuQueryService;
	@Autowired
	TjZheXianQueryService tjZheXianQueryService;
	
	@Autowired
	DataStatisticsService dataService;
	
	@RequestMapping(value = {"index"})
	public String index(User user, Model model) {
		return "xrlh/tj/tjIndex";
	}	
	
	@RequestMapping(value = "frameView")
	public String formview(Office office, HttpServletRequest request, HttpServletResponse response, Model model) {
		office = officeService.get(office.getId());
		model.addAttribute("office", office);
		return "xrlh/tj/frameView";
	}

	@RequestMapping(value = "zhuTu")
	public String zhuTu(Office office, HttpServletRequest request, HttpServletResponse response, Model model) {
		TjZhuQuery query = new TjZhuQuery();
		query.setOfficeId(office.getId());
		TjZhu tj = new TjZhu();
		office = officeService.get(office.getId());
		List<TjZhuQuery> list = tjZhuQueryService.findlist(query);
		tj.setOffice(office);
		List<String> legend = Lists.newArrayList();
		legend.add("参与执法人员数");
		legend.add("参与执法对象数");
		
		List<String> xAxis = Lists.newArrayList();
		for(TjZhuQuery zhu:list){
			xAxis.add(zhu.getMonthName());
		}

		List<Series> seriesList = Lists.newArrayList();
		
		//for(String tarName:legend){
		for(int i=0;i<legend.size();i++){
			Series series = new Series();
			series.setName(legend.get(i));
			series.setType("bar");
			List<Integer> data = Lists.newArrayList();
			for(TjZhuQuery zhu:list){
				if(legend.get(i).equals("参与执法人员数")){
					data.add(zhu.getRysl().intValue());
				}else{
					data.add(zhu.getDxsl().intValue());
				}
			}
			series.setData(data);
			seriesList.add(series);
		}
		tj.setLegend(legend);
		tj.setxAxis(xAxis);
		tj.setSeriesList(seriesList);
		model.addAttribute("office", office);
		model.addAttribute("tj", tj);
		return "xrlh/tj/ZhuTuView";
	}
	
	@RequestMapping(value = "zheXian")
	public String zheXian(Office office, HttpServletRequest request, HttpServletResponse response, Model model) {
		TjZheXianQuery query = new TjZheXianQuery();
		query.setOfficeId(office.getId());
		TjZhu tj = new TjZhu();
		office = officeService.get(office.getId());
		List<TjZheXianQuery> list = tjZheXianQueryService.findlist(query);
		tj.setOffice(office);
		List<String> legend = Lists.newArrayList();
		legend.add("问题数量");
		legend.add("问题对象数量");
		
		List<String> xAxis = Lists.newArrayList();
		for(TjZheXianQuery xian:list){
			xAxis.add(xian.getMonthName());
		}

		List<Series> seriesList = Lists.newArrayList();
		
		//for(String tarName:legend){
		for(int i=0;i<legend.size();i++){
			Series series = new Series();
			series.setName(legend.get(i));
			series.setType("bar");
			List<Integer> data = Lists.newArrayList();
			for(TjZheXianQuery zhe:list){
				if(legend.get(i).equals("问题数量")){
					data.add(zhe.getWtsl()==null?0:zhe.getWtsl().intValue());
				}else{
					data.add(zhe.getWtqysl()==null?0:zhe.getWtqysl().intValue());
				}
			}
			series.setData(data);
			seriesList.add(series);
		}
		tj.setLegend(legend);
		tj.setxAxis(xAxis);
		tj.setSeriesList(seriesList);
		model.addAttribute("office", office);
		model.addAttribute("tj", tj);
		return "xrlh/tj/ZhexianView";
	}
	
	
	@RequestMapping(value = "dataStatistics")
	public String dataStatistics(Office office, HttpServletRequest request, HttpServletResponse response, Model model) {
		
		//总局事项
		DataStatistics zjSx = dataService.queryZj_SX().get(0);
		//直属局事项
		DataStatistics zsjSx = dataService.queryZsj_SX_Main().get(0);
		//直属局事项分组
		List<DataStatistics> zsjSx_fz = dataService.queryZsj_SX_Detail();
		//分支机构事项
		DataStatistics fzjgSx = dataService.queryFzjg_SX_Main().get(0);
		//分支机构事项分组
		List<DataStatistics> fzjgSx_fz = dataService.queryFzjg_SX_Detail();
		//---------------------------------------------------------------------
		//日常施检事项
		DataStatistics rcSx = dataService.queryRc_SX_Main().get(0);
		//日常施检事项分组
		List<DataStatistics> rcSx_fz = dataService.queryRc_SX_Detail();
		//---------------------------------------------------------------------
		//总局计划
		DataStatistics zjJh = dataService.queryZj_JH().get(0);
		//直属局计划
		DataStatistics zsjJh = dataService.queryZsj_JH_Main().get(0);
		//直属局计划分组
		List<DataStatistics> zsjJh_fz = dataService.queryZsj_JH_detail();
		//分支机构计划
		DataStatistics fzjgJh = dataService.queryFzjg_JH_Main().get(0);
		//分支机构计划分组
		List<DataStatistics> fzjgJh_fz = dataService.queryFzjg_JH_detail();
		
		//日常计划
		DataStatistics rcJh = dataService.queryRc_JH_Main().get(0);
		//日常计划分组
		List<DataStatistics> rcJh_fz = dataService.queryRc_JH_detail();
		
		
		//总局任务数
		DataStatistics zjRw = dataService.queryZj_RW().get(0);
		//直属局任务数
		DataStatistics zsjRw = dataService.queryZsj_RW().get(0);
		//直属局任务数分组
		List<DataStatistics> zsjRw_fz = dataService.queryZsj_RW_detail();
		//分支机构任务数
		DataStatistics fzjgRw = dataService.queryFzjg_RW().get(0);
		//分支机构任务数分组
		List<DataStatistics> fzjgRw_fz = dataService.queryFzjg_RW_detail();

		//分支机构任务数
		DataStatistics rcRw = dataService.queryRc_RW().get(0);
		//分支机构任务数分组
		List<DataStatistics> rcRw_fz = dataService.queryRc_RW_detail();
		
		
		//人员库库存
		DataStatistics kc_ry = dataService.queryKc_Ry().get(0);
		//对象库库存
		DataStatistics kc_dx = dataService.queryKc_Dx().get(0);
		//人员库选中
		DataStatistics xz_ry = dataService.queryXz_Ry().get(0);
		//对象库选中
		DataStatistics xz_dx = dataService.queryXz_Dx().get(0);
		
		//人员在库详细
		List<DataStatistics> kc_ry_xx = dataService.queryKc_Ry_xx();
		//对象在库详细
		List<DataStatistics> kc_dx_xx = dataService.queryKc_Dx_xx();
		//人员在库详细
		List<DataStatistics> xz_ry_xx = dataService.queryXz_Ry_xx();
		//对象在库详细
		List<DataStatistics> xz_dx_xx = dataService.queryXz_Dx_xx();
		
		model.addAttribute("zjSx", zjSx);
		model.addAttribute("zsjSx", zsjSx);
		model.addAttribute("zsjSx_fz", zsjSx_fz);
		model.addAttribute("fzjgSx", fzjgSx);
		model.addAttribute("fzjgSx_fz", fzjgSx_fz);
		
		model.addAttribute("zjJh", zjJh);
		model.addAttribute("zsjJh", zsjJh);
		model.addAttribute("zsjJh_fz", zsjJh_fz);
		model.addAttribute("fzjgJh", fzjgJh);
		model.addAttribute("fzjgJh_fz", fzjgJh_fz);
		
		model.addAttribute("zjRw", zjRw);
		model.addAttribute("zsjRw", zsjRw);
		model.addAttribute("zsjRw_fz", zsjRw_fz);
		model.addAttribute("fzjgRw", fzjgRw);
		model.addAttribute("fzjgRw_fz", fzjgRw_fz);
		
		model.addAttribute("kc_ry", kc_ry);
		model.addAttribute("kc_dx", kc_dx);
		model.addAttribute("xz_ry", xz_ry);
		model.addAttribute("xz_dx", xz_dx);
		
		model.addAttribute("kc_ry_xx", kc_ry_xx);
		model.addAttribute("kc_dx_xx", kc_dx_xx);
		model.addAttribute("xz_ry_xx", xz_ry_xx);
		model.addAttribute("xz_dx_xx", xz_dx_xx);
		
		model.addAttribute("rcSx", rcSx);
		model.addAttribute("rcSx_fz", rcSx_fz);
		model.addAttribute("rcJh", rcJh);
		model.addAttribute("rcJh_fz", rcJh_fz);
		model.addAttribute("rcRw", rcRw);
		model.addAttribute("rcRw_fz", rcRw_fz);
		return "xrlh/tj/dataStatistics";
	}
}